package com.fyzk.activity.lottery.shuzicai.klpk;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.bean.model.SsqLiShiJX;
import com.tyjc.lottery.R;
import com.fyzk.utils.AppUtil;
import com.fyzk.utils.LogUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class KLPKHistoryAdapter extends BaseAdapter{

	private List<SsqLiShiJX> liShi;
	private Context context;

	private int[] imgs = new int[]{R.drawable.klpk_kj_1, R.drawable
			.klpk_kj_2, R.drawable.klpk_kj_3,R.drawable.klpk_kj_4};
	private List<Bitmap> bitmaps = new ArrayList<Bitmap>();

	public KLPKHistoryAdapter(List<SsqLiShiJX> bellow_lishi, Context context){
		liShi = bellow_lishi;
		this.context = context;
		
		for (int i = 0; i < imgs.length; i++) {
			Bitmap bit = BitmapFactory.decodeResource(context.getResources(), imgs[i]);
			bitmaps.add(bit);
		}
	}

	@Override
	public int getCount() {
		if (liShi == null) {
			return 0;
		}
		return liShi.size();
	}

	@Override
	public Object getItem(int position) {

		return null;
	}

	@Override
	public long getItemId(int position) {

		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		SsqLiShiJX ssqLiShiJX = liShi.get(position);
		
		View view = LayoutInflater.from(context).inflate(R.layout.klpkhistory_listview_layout, null);
		LinearLayout b_ll_list = (LinearLayout)view.findViewById(R.id.klpkhistory_ll);
		b_ll_list.getLayoutParams().height = AppUtil.getHeight()*25/560;
		LinearLayout nums_ll = (LinearLayout)view.findViewById(R.id.klpkhistory_nums_ll);
		TextView issue_tv = (TextView)view.findViewById(R.id.klpkhistory_issue);

		String issue = ssqLiShiJX.getQi();
		String prizeNum = ssqLiShiJX.getHongQiu();
		List<Integer> prizeIds = getPrizeNum(prizeNum);
		try {
			issue_tv.setText(issue.subSequence(issue.length() - 3, issue.length()));
			nums_ll.removeAllViews();
			nums_ll.addView(KLPKPrizeTools.getNumView(prizeIds));
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		
		return view;
	}

	public void setList(List<SsqLiShiJX> liShi){
		this.liShi = liShi;
		notifyDataSetChanged();
	}
	
	private List<Integer> getPrizeNum(String prizeNum){
		List<Integer> prizeIds = new ArrayList<Integer>();
		if (prizeNum.contains(" ")) {
			String[] p = prizeNum.split(" ");
			for (int i = 0; i < p.length; i++) {
				int pri = 0;
				try {
					pri = Integer.parseInt(p[i]);
				} catch (Exception e) {
					// TODO: handle exception
				}
				prizeIds.add(pri);
			}
		}
		Collections.sort(prizeIds);
		return prizeIds;
	}

}
